﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormStore.Basic
{
    public partial class FrmType : Form
    {
        public FrmType()
        {
            InitializeComponent();
        }
        private void BindData()
        {
            DBHelper.PrepareSql("select * from [Type]");
            this.dataGridView1.DataSource = DBHelper.ExecuteQuery();
        }
        private void FrmType_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string sql = "insert into [Type](TypeName) values(@TypeName)";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("TypeName", this.txtType.Text);
            int rowcount = DBHelper.ExecuteNonQuery();
            if (rowcount == 1)
            {
                MessageBox.Show("数据添加成功！");
                BindData();
            }
            else
            {
                MessageBox.Show("数据添加失败！");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int typeid = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            string sql = "update [Type] set TypeName=@TypeName where TypeID=@TypeID";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("TypeName", this.txtType.Text);
            DBHelper.SetParameter("TypeID", typeid);

            int rowcount = DBHelper.ExecuteNonQuery();
            if (rowcount == 1)
            {
                MessageBox.Show("数据修改成功！");
                BindData();
            }
            else
            {
                MessageBox.Show("数据修改失败！");
            }
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            this.txtType.Text = dt.Rows[0]["TypeName"].ToString();
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows[0].Cells[0].Value == null)
            {
                MessageBox.Show("您未选择数据！");
            }
            else
            {
                int id = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                DBHelper.PrepareSql("select * from [Type] where TypeId=@TypeId");
                DBHelper.SetParameter("TypeId", id);
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery();
                if (dt.Rows.Count != 1)
                {
                    MessageBox.Show("获取不到数据");
                    BindData();
                    return;
                }
                else
                {
                    this.txtType.Text = dt.Rows[0]["TypeName"].ToString();
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            
            DialogResult result = MessageBox.Show("确定要删除数据吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                int typeid = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                string sql = "delete from [Type] where TypeId=@TypeId";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("TypeID", typeid);
                int rowcount = DBHelper.ExecuteNonQuery();
                if (rowcount == 1)
                {

                    MessageBox.Show("数据删除成功！");
                    BindData();
                }
                else
                {
                    MessageBox.Show("数据删除失败！");
                }   
            }
            
               
        }
    }
}
